<script setup lang="ts">
import AddWrite from './components/addWrite.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref, unref } from 'vue'
import { ElButton } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useRouter } from 'vue-router'
import { empAdd } from '@/api/common'
import { useEmitt } from '@/hooks/web/useEmitt'

const { emitter } = useEmitt()

const { push } = useRouter()

const { t } = useI18n()

const addWrite = ref<ComponentRef<typeof AddWrite>>()

const loading = ref(false)

const save = async () => {
  const write = unref(addWrite)
  console.log('---addWrite---', write?.getFormData())

  let data = {
    username: write?.getFormData()?.username,
    password: write?.getFormData()?.password,
    name: write?.getFormData()?.name,
    dept_id: write?.getFormData()?.dept_id,
    code: write?.getFormData()?.code,
    workplace: write?.getFormData()?.workplace,
    job: write?.getFormData()?.job,
    mobile: write?.getFormData()?.mobile,
    phone: write?.getFormData()?.phone,
    birth: write?.getFormData()?.birth,
    entrytime: write?.getFormData()?.entrytime,
    cardno: write?.getFormData()?.cardno,
    gender: write?.getFormData()?.gender,
    census: write?.getFormData()?.census,
    census_type: write?.getFormData()?.census_type,
    census_address: write?.getFormData()?.census_address,
    address: write?.getFormData()?.address,
    politics: write?.getFormData()?.politics,
    marital: write?.getFormData()?.marital,
    bear: write?.getFormData()?.bear,
    education: write?.getFormData()?.education,
    schooltag: write?.getFormData()?.schooltag,
    major: write?.getFormData()?.major,
    pactstart: write?.getFormData()?.pactstart,
    pactend: write?.getFormData()?.pactend,
    exigency: write?.getFormData()?.exigency,
    exigency_mobile: write?.getFormData()?.exigency_mobile,
    bank: write?.getFormData()?.bank,
    bankno: write?.getFormData()?.bankno,
    remark: write?.getFormData()?.remark,
    social_security: write?.getFormData()?.social_security,
    social_security_time: write?.getFormData()?.social_security_time,
    insuredtime: write?.getFormData()?.insuredtime
  }

  // data.buytime = '' + parseInt(data.buytime) / 1000
  console.log('---客户新增参数---', data)
  const res = await empAdd(data)
  if (res) {
    emitter.emit('getList', 'add')
    push('/personnelManagement/staff')
  }
}
</script>

<template>
  <ContentDetailWrap :title="t('exampleDemo.add')" @back="push('/personnelManagement/staff')">
    <!-- <Write ref="writeRef" /> -->
    <AddWrite ref="addWrite" />

    <template #right>
      <ElButton type="primary" :loading="loading" @click="save">
        {{ t('exampleDemo.save') }}
      </ElButton>
    </template>
  </ContentDetailWrap>
</template>
